package com.aaa.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: upload
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/7/27 16:15
 * @Version:1.0
 **/
public class upload {
    @Value("${filepath}")
    String filepath;
    public String upload(MultipartFile head){

        String cc="";
        String originalFilename = head.getOriginalFilename();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHssmm");
        String format = now.format(formatter);
//        文件格式
        String n1=originalFilename.substring(originalFilename.lastIndexOf("."));
        if(!"".equals(originalFilename)){
            String s1 = format + n1;
            String s = "D:/img/" + s1;
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>"+s);
            File file = new File(s);
            if (!file.exists()) {//如果目录不存在，创建这样一个目录；
                file.mkdir();
            }
            try {
                head.transferTo(file);
                cc=s1;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return cc;
    }
    public String[] multiUpload(MultipartFile[] head){
        String[] arr=new String[head.length];
        System.out.println("文件的个数:"+head.length);
        int i=0;
        for (MultipartFile f : head){
            String originalFilename = f.getOriginalFilename();
            String n1=originalFilename.substring(originalFilename.lastIndexOf("."));

            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHssmm");
            String format = now.format(formatter);
            if(!"".equals(originalFilename)){
                String s =  format+System.currentTimeMillis()+ n1;
                File file = new File("D:/img/" +s);
                try {
                    f.transferTo(file);
                    arr[i]=s;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("arr["+i+"]--------"+arr[i]);
            i++;
        }
        return arr;
    }
//    文件下载

    public void download(HttpServletResponse response) throws Exception {
        // 文件地址，真实环境是存放在数据库中的
        File file = new File("D:/file.txt");
        // 穿件输入对象
        FileInputStream fis = new FileInputStream(file);
        // 设置相关格式
        response.setContentType("application/force-download");
        // 设置下载后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + "a.txt");
        // 创建输出对象
        OutputStream os = response.getOutputStream();
        // 常规操作
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = fis.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        fis.close();
    }

}
